<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>CollectStart</title>
	<style>
		body {
			margin: 0;
			padding: 0;
		}
	</style>
	<script src="../phaser.min.js"></script>
</head>
<body>
	<div id="parent"></div>
	<script>
		//游戏界面 宽高 渲染方式 父级元素 于对象中创建方法
		var game = new Phaser.Game(800,600,Phaser.CANVAS,'parent',{
			preload:preload,//资源加载
			create:create,//创建场景
			update:update//游戏循环
		});
		function preload(){
			console.log('资源加载');
			game.load.image('star','assets/star.png');//图片加载 星星
			game.load.image('sky','assets/sky.png');//背景
			game.load.image('ground','assets/platform.png');//地板
			game.load.spritesheet('dude','assets/dude.png',32,48);//加载精灵图 角色
//预加载//////////////////////////////////////////////////////////////////
			game.load.image('diamond','assets/diamond.png');//钻石
			game.load.image('heart','assets/heart.png');//心
			game.load.image('aid','assets/firstaid.png');//援助
			game.load.spritesheet('baddie','assets/baddie.png',32,32);//反派角色
			game.load.audio("badboom", "assets/badboom.mp3");
			game.load.audio("aidboom", "assets/aidboom.mp3");
////////////////////////////////////////////////////////////////////
		}
		var platforms;//地板组合
		var stars;
		var player;
//定义变量/////////////////////////////////
		var aid;
		var bads;
		var bad=[];
		var left=[];
		var hp=3;
		var _hp=0;//消除心所用
		var diamond;
		var heart;
		var badboom;
		var aidboom;
///////////////////////////////////
		var cursors;//输入
		var score=0;//分数
		var scoreText;
		function create(){
			console.log('创建场景');
			game.physics.startSystem(Phaser.ARCADE);//启动ARCADE物理引擎
			game.add.sprite(0,0,'sky');
			platforms=game.add.group();
			platforms.enableBody=true;//启动组的物理引擎

			//game.add.sprite(400,600-32,'ground');//game.world.height获取世界高度
			var ledge=platforms.create(-150,250,'ground');
			ledge.body.immovable=true;
			ledge=platforms.create(400,400,'ground');
			ledge.body.immovable=true;
			//var ground = game.add.sprite(0,600-32,'ground');//返回当前精灵对象
			var ground = platforms.create(0,600-32,'ground');
			ground.scale.setTo(2,1);
			ground.body.immovable=true;//不可移动
			//game.add.sprite(0,0,'star');//精灵创建 坐标 对象
			player=game.add.sprite(32,32,'dude');
			game.physics.arcade.enable(player);//创建角色的物理属性
			player.body.gravity.y=300;//设置角色的重力
			player.body.collideWorldBounds=true;//碰撞世界范围
			player.animations.add('left',[0,1,2,3],10,true);//创建动画 数组为要播放的帧序号(精灵图) 10毫秒 是否循环
			player.animations.add('right',[5,6,7,8],10,true);
//创建场景-急救包、钻石、反派角色///////////////////////////////////////////
			diamond=game.add.sprite(0,0,'diamond');
			heart=game.add.group();
			for (var i = 0; i < 3; i++) {
				heart.create(300+i*24,0,'heart');
				heart.scale.setTo(2,2);
			}
			diamond.x=-diamond.width;
			game.physics.arcade.enable(diamond);
			aid=game.add.sprite(RandomNum(400,800-32),400-32,'aid');
			game.physics.arcade.enable(aid);
			bads=game.add.group();
			bads.enableBody=true;
			for (var i = 0; i<3; i++) {
				bad[i] = bads.create(i*game.world.width/3,game.world.height-32*2,'baddie');
			}
				bad[3] = bads.create(RandomNum(400,800-32-150),400-32,'baddie');
				bad[4] = bads.create(RandomNum(50,400-150-150-32),250-32,'baddie');
			for (var i = 0; i<5; i++) {
				bad[i].animations.add('left',[0,1],10,true);
				bad[i].animations.add('right',[2,3],10,true);	
				left[i]=bad[i].x;
			}
			badboom=game.add.audio("badboom");
			aidboom=game.add.audio("aidboom");
/////////////////////////////////////////////
			//创建按键 方向
			cursors=game.input.keyboard.createCursorKeys();

			stars=game.add.group();
			stars.enableBody=true;
			for (var i = 0; i<12; i++) {
				var star = stars.create(i*70,0,'star');
				star.body.gravity.y=300;
				star.body.bounce.y=0.7+Math.random()*0.2;//落地的弹性系数
			}
			//坐标 文本 样式
			scoreText=game.add.text(16,16,'score：'+ score,{fontSize:'32px',fill:'#000'});
		}
		var x=1;
		var direction='right';
		function update(){
			console.log('游戏循环');
			game.physics.arcade.collide(player,platforms);//碰撞检测
			game.physics.arcade.collide(stars,platforms);
			//game.physics.arcade.collide(stars,player);//顶开...无法收集 推箱子之类的可以用
			game.physics.arcade.overlap(stars,player,collectStar,null,this);
			if (cursors.left.isDown) {
				player.body.velocity.x=-150;
				player.animations.play('left');
			} else if (cursors.right.isDown) {
				player.body.velocity.x=150;
				player.animations.play('right');
			} else {
				player.body.velocity.x=0;//加上这个判断则没按一次运动一次否则直接滑翔
				player.frame=4;//精灵图第4帧
			}
			if (cursors.up.isDown && player.body.touching.down) {
				//当按下up键并下部碰撞时（着地）
				player.body.velocity.y=-350;
			}
//游戏循环-碰撞检测//////////////////////////////////////////////////
			game.physics.arcade.collide(player,diamond,function(){
				diamond.kill();
				alert('you are won!');
				location.reload();
			});
			game.physics.arcade.collide(player,aid,function(){
				aid.kill();
				heart.create(300+3*24,0,'heart');
				heart.scale.setTo(2,2);
				hp+=1;
				aidboom.play();
			});
			game.physics.arcade.overlap(bads,player,isDead,null,this);
			for (var i = 0; i < bad.length; i++) {
				bad[i].x+=x;
				bad[i].animations.play(direction);
				if (bad[i].x>=left[i]+150){
					x=-x;
					direction='left';
				}
				if (bad[i].x<left[i]){
					x=-x;
					direction='right';
				}
			}
////////////////////////////////////////////////////
		}
		function collectStar(player,star){
			star.kill();//消除星星
			score+=1;
			scoreText.text='score：' + score;
//分数12出现钻石/////////////////////////
			if (score==12) {
				diamond.x=game.world.width/2-16;
				diamond.y=game.world.height/3-14;
			}
///////////////////////////
		}
//自定义函数--随机数、判断死亡////////////////////////////////////////////
		function RandomNum(Min,Max){
			var Range = Max - Min;
			var Rand = Math.random();   
			var num = Min + Math.round(Rand * Range);
			return num;
		}
		function isDead(player,bads){
			badboom.play();
			bads.kill();
			heart.getChildAt(_hp).kill();
			hp-=1;
			_hp+=1;
			if (hp==0) {
				alert('you are dead!');
				location.reload();
			}
		}
//////////////////////////////////////////////
	</script>
</body>
</html>